package cn.tedu.trading_platform.controller;

import cn.tedu.trading_platform.base.response.JsonResult;
import cn.tedu.trading_platform.base.response.StatusCode;
import cn.tedu.trading_platform.mapper.ProductsMapper;
import cn.tedu.trading_platform.pojo.dto.AddProductParam;
import cn.tedu.trading_platform.pojo.entity.Product;
import cn.tedu.trading_platform.pojo.vo.MerchantsVO;
import cn.tedu.trading_platform.pojo.vo.ProductVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/product/")
@Api(tags = "商品管理模块")
public class ProductController {
    @Autowired
    private ProductsMapper productsMapper;
    @PostMapping("add")
    @ApiOperation("添加商品")
    @ApiOperationSupport(order=1)
    public JsonResult addProduct(@Validated @RequestBody AddProductParam addProductParam,@ApiIgnore HttpSession session){
        log.debug("开始处理添加商品请求");
        MerchantsVO merchantsVO = (MerchantsVO) session.getAttribute("merchants");
        if(merchantsVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        Product product = new Product();
        product.setMerchantsId(merchantsVO.getId());
        System.out.println(product.getId());
        BeanUtils.copyProperties(addProductParam,product);
        product.setCreatedAt(new Date());
        productsMapper.insertProduct(product);
        return JsonResult.ok();
    }

    @PostMapping("select")
    @ApiOperation("查询商品")
    @ApiOperationSupport(order=2)
    public JsonResult selectProduct(HttpSession session){
        log.debug("开始处理查询商品请求");
        MerchantsVO merchantsVO = (MerchantsVO) session.getAttribute("merchants");
        if(merchantsVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        List<ProductVO> productVOS = productsMapper.selectProduct(merchantsVO.getId());
        return JsonResult.ok(productVOS);
    }

    @PostMapping("del")
    @ApiOperation("删除商品")
    @ApiOperationSupport(order=3)
    public JsonResult delProduct(Long id,HttpSession session){
        log.debug("开始处理删除商品请求");
        MerchantsVO merchantsVO = (MerchantsVO) session.getAttribute("merchants");
        if(merchantsVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        int i = productsMapper.deleteProduct(id);
        if(i==0){
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
        return JsonResult.ok();
    }
    @PostMapping("selectByName")
    @ApiOperation("根据商品名查询商品")
    @ApiOperationSupport(order=4)
    public JsonResult selectProductByName(String name){
        log.debug("开始处理根据商品名查询商品请求");
        List<ProductVO> productVOS = productsMapper.selectProductByName(name);
        return JsonResult.ok(productVOS);
    }
    @PostMapping("selectByCategory")
    @ApiOperation("根据商品类别查询商品")
    @ApiOperationSupport(order=5)
    public JsonResult selectProductByCategory(String category)
    {
        log.debug("开始处理根据商品类别查询商品请求");
        List<ProductVO> productVOS = productsMapper.selectProductByCategory(category);
        return JsonResult.ok(productVOS);
    }
    @PostMapping("selectId")
    @ApiOperation("查询商品id")
    @ApiOperationSupport(order=6)
    public JsonResult selectProductId(){
        log.debug("开始处理查询商品id请求");
        return JsonResult.ok(productsMapper.selectProduct(1L));
    }
}
